#!/usr/bin/env bash

source "$(dirname "$0")/common.sh"
check_requirements

#-------------------------------------------------------------------------------
function usage() {
   echo "Make a Kong patch release using this script:"
   echo ""
   echo "Usage:"
   if [ "$version" = "<x.y.z>" ]
   then
      echo "     List executed steps for a given release"
      echo "        $0 $version $1 $3"
      echo
   fi
   c=1
   step "check_milestone"      "ensure all PRs marked on the release milestone are 100% merged"
   step "check_dependencies"   "ensure all kong dependencies are bumped in the rockspec"
   step "create"               "create the branch"
   step "write_changelog"      "prepare the changelog"
   step "commit_changelog"     "commit the changelog"
   step "update_copyright"     "update copyright file"
   step "update_admin_api_def" "update Admin API definition"
   step "version_bump"         "bump and commit the version number"
   step "submit_release_pr"    "push and submit a release PR"
   step "docs_pr"              "push and submit a docs.konghq.com PR for the release"
   step "merge"                "merge, tag and sign the release"
   step "approve_docker"       "get humans to review and approve machine-provided pull request at docker-kong repo"
   step "merge_docker"         "merge, tag and sign Kong's docker-kong PR"
   step "submit_docker"        "submit a PR to docker-library/official-images"
   step "merge_homebrew"       "humans approve and merge machine PR to homebrew-kong"
   step "upload_luarock"       "upload to LuaRocks" "<api-key>"
   step "merge_vagrant"        "humans approve and merge machine PR to kong-vagrant"
   step "merge_pongo"          "humans approve and merge machine PR to kong-pongo"
   step "announce"             "Get announcement messages for Kong Nation and Slack #general"
   exit 0
}


#-------------------------------------------------------------------------------
# Default help
#-------------------------------------------------------------------------------

if [ "$1" = "-h" ] || [ "$1" = "--help" ] || ! [ "$1" ]
then
   version="<x.y.z>"
   usage "$@"
fi

#-------------------------------------------------------------------------------
# Variables
#-------------------------------------------------------------------------------

version="$1"
step="$2"

major=${version%%.*}
rest=${version#*.}
minor=${rest%%.*}
patch=${rest##*.}
rockspec="kong-$version-0.rockspec"
branch="release/$version"
base="release/$major.$minor.x"

if ! [[ "$version" =~ ^[0-9]+.[0-9]+.[0-9]$ ]]
then
   die "first argument must be a version in x.y.z format"
fi

if [ "$step" = "" ]
then
   usage "$@"
fi

EDITOR="${EDITOR-$VISUAL}"

case "$step" in
   check_dependencies) check_dependencies ;;

   check_milestone) check_milestone ;;

   #---------------------------------------------------------------------------
   create)
      if [ $(git status --untracked-files=no --porcelain | wc -l) != "0" ]
      then
         die "Local tree is not clean, please commit or stash before running this."
      fi

      set -e
      git checkout "$base"
      git pull
      git checkout -B "$branch"

      SUCCESS "Release branch was created locally." \
              "Ensure to cherry-pick all required changes into $branch." \
              "And proceed to the next step:" \
              "    $0 $version cherry_pick"
      ;;
   #---------------------------------------------------------------------------
   write_changelog) write_changelog "$version" ;;
   commit_changelog) commit_changelog "$version" ;;
   update_copyright) update_copyright "$version" ;;
   update_admin_api_def) update_admin_api_def "$version" ;;

   #---------------------------------------------------------------------------
   version_bump)
      if ! grep -q "patch = $patch" kong/meta.lua
      then
         sed -i.bak 's/patch = [0-9]*/patch = '$patch'/' kong/meta.lua
         git add kong/meta.lua
      fi
      if ! [ -f "$rockspec" ]
      then
         git mv kong-*-0.rockspec "$rockspec"
         sed -i.bak 's/^version = ".*"/version = "'$version'-0"/' "$rockspec"
         sed -i.bak 's/^  tag = ".*"/  tag = "'$version'"/' "$rockspec"
      fi

      git status
      git diff

      CONFIRM "If everything looks all right, press Enter to make the release commit" \
              "or Ctrl-C to cancel."

      git add $rockspec

      git commit -m "release: $version"
      git log -n 1

      SUCCESS "Version bump for the release is now committed locally." \
              "You are ready to run the next step:" \
              "    $0 $version submit_release_pr"
      ;;

   #---------------------------------------------------------------------------
   submit_release_pr) submit_release_pr "$branch" "$version" ;;

   #---------------------------------------------------------------------------
   merge)
      CONFIRM "Press Enter to merge the PR into master and push the tag and Github release" \
              "or Ctrl-C to cancel."

      set -e
      git checkout "$branch"
      git pull
      git checkout master
      git pull
      git merge "$branch"
      git push
      git tag -s "$version" -m "$version"
      git push origin "$version"

      make_github_release_file

      hub release create -F release-$version.txt "$version"
      rm -f release-$version.txt

      SUCCESS "Make sure the packages are built and available on download.konghq.com" \
              "before continuing to the following steps." \

              "They should be visible on https://internal.builds.konghq.com/job/kong/view/tags/. " \
              "An recurrent task checks for new releases every 15 minutes on the server. " \
              "If needed, the link 'Scan Multibranch Pipeline Now' will scan on-demmand. It can be used " \
              "to attempt to rebuild, if there was an error."

              "As the packages are built, you may run the following steps in parallel:" \
              "* 'upload_luarock'" \
              "* 'merge_homebrew'" \
              "* 'merge_vagrant'" \
              "* 'merge_pongo'" \
              "* 'approve_docker', then 'merge_docker', then 'submit_docker'"
      ;;
   #---------------------------------------------------------------------------
   docs_pr) docs_pr "$branch" ;;
   approve_docker) approve_docker ;;
   merge_docker) merge_docker "$branch" "$version" ;;
   submit_docker) submit_docker "$version";;
   merge_homebrew)merge_homebrew ;;
   merge_pongo) merge_pongo ;;
   merge_vagrant) merge_vagrant ;;
   upload_luarock) upload_luarock "$rockspec" "$3" ;;
   announce) announce "$major" "$minor" "$patch" ;;
   *)
      die "Unknown step!"
      ;;
esac
